package yf.mall.controller.api.app;

import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.service.UserService;
import yf.mall.service.app.AppUserService;
import yf.mall.service.app.LoginService;
import yf.mall.vo.app.SignOffVo;
import yf.mall.vo.app.UserUpdateDTO;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/app/mini/user")
public class ApiAppUserController {

    @Resource
    private UserService userService;

    @Resource
    private AppUserService appUserService;

    @Resource
    private LoginService loginService;

    @PostMapping("/update")
    public R update(@LoginUserId Long userId, @RequestBody @Validated UserUpdateDTO userUpdateDTO) {
        userService.update(userId, userUpdateDTO);
        return R.ok();
    }

    @PostMapping("/signoff")
    private R signOff(@LoginUserId Long userId, SignOffVo signOffVo) {

        boolean logout = loginService.logout(userId);

        if (logout) {
            boolean result = appUserService.signOff(userId, signOffVo != null ? signOffVo.getReason() : "");
            return R.ok(result);
        }
        return R.ok(false);
    }

}
